/*
 * Copyright (C) 2008 Mads Mohr Christensen
 * Copyright (C) 2008 Ewan Davies
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 * hr.mohr@gmail.com
 * Mads Mohr Christensen 2008
 * 
 * Based on Javascript written by Jaap Scherphuis. (jaapsch a t yahoo d o t com)
 */
package rubiksjtimer;

public class ScrambleClock extends Scramble 
{
	
	private static final int[] position = {0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0};
	private static final int[][] moves = {
			{1,1,1,1,1,1,0,0,0,  -1,0,-1,0,0,0,0,0,0},
			{0,1,1,0,1,1,0,1,1,  -1,0,0,0,0,0,-1,0,0},
			{0,0,0,1,1,1,1,1,1,  0,0,0,0,0,0,-1,0,-1},
			{1,1,0,1,1,0,1,1,0,  0,0,-1,0,0,0,0,0,-1},

			{0,0,0,0,0,0,1,0,1,  0,0,0,-1,-1,-1,-1,-1,-1},
			{1,0,0,0,0,0,1,0,0,  0,-1,-1,0,-1,-1,0,-1,-1},
			{1,0,1,0,0,0,0,0,0,  -1,-1,-1,-1,-1,-1,0,0,0},
			{0,0,1,0,0,0,0,0,1,  -1,-1,0,-1,-1,0,-1,-1,0},

			{0,1,1,1,1,1,1,1,1,  -1,0,0,0,0,0,-1,0,-1},
			{1,1,0,1,1,1,1,1,1,  0,0,-1,0,0,0,-1,0,-1},
			{1,1,1,1,1,1,1,1,0,  -1,0,-1,0,0,0,0,0,-1},
			{1,1,1,1,1,1,0,1,1,  -1,0,-1,0,0,0,-1,0,0},

			{1,1,1,1,1,1,1,1,1,  -1,0,-1,0,0,0,-1,0,-1},
			{1,0,1,0,0,0,1,0,1,  -1,-1,-1,-1,-1,-1,-1,-1,-1}
	};
	
	private int[] sequence = new int[14];
	
	public ScrambleClock() 
	{
		super();
	}

	public String getName() 
	{
		return "Clock";
	}
		
	private char adjustPin() 
	{
		int direction = (int) (Math.abs(generator.nextInt()) % 3);
		return (direction==0) ? 'U' : 'd';
	}
	
	public String generateScramble() 
	{
		// TODO get a size cap on clock scrambles
		StringBuffer buf = new StringBuffer(200);

		// generate a random sequence
		for (int i = 0; i < 14; i++)
		{
			sequence[i] = (int) (Math.abs(generator.nextInt()) % 12) - 5;
		}
		
		// make an array of random moves
		for (int i = 0; i < 14; i++)
		{
			for (int j = 0; j < 18; j++)
			{
				position[j] += sequence[i] * moves[i][j];
			}
		}
		
		// modulus function for position
		for (int j = 0; j < 18; j++)
		{
			position[j] %= 12;
			while (position[j] <= 0) position[j] += 12;
		}

		buf.append("UU  u="); 	buf.append(sequence[0]); 	buf.append("\n");
		buf.append("dd  d="); 	buf.append(sequence[4]); 	buf.append("\n");
		buf.append("\n");
		buf.append("dU  u=");	buf.append(sequence[1]);	buf.append("\n");
		buf.append("dU  d=");	buf.append(sequence[5]);	buf.append("\n");
		buf.append("\n");
		buf.append("dd  u=");	buf.append(sequence[2]);	buf.append("\n");
		buf.append("UU  d=");	buf.append(sequence[6]); 	buf.append("\n");
		buf.append("\n");
		buf.append("Ud  u=");	buf.append(sequence[3]);	buf.append("\n");
		buf.append("Ud  d=");	buf.append(sequence[7]);	buf.append("\n");
		buf.append("\n");
		buf.append("dU  u=");	buf.append(sequence[8]);	buf.append("\n");
		buf.append("UU\n");
		buf.append("\n");
		buf.append("Ud  u=");	buf.append(sequence[9]);	buf.append("\n");
		buf.append("UU\n");
		buf.append("\n");
		buf.append("UU  u=");	buf.append(sequence[10]);	buf.append("\n");
		buf.append("Ud\n");
		buf.append("\n");
		buf.append("UU  u=");	buf.append(sequence[11]);	buf.append("\n");
		buf.append("dU\n");
		buf.append("\n");
		buf.append("UU  u=");	buf.append(sequence[12]);	buf.append("\n");
		buf.append("UU\n");
		buf.append("\n");
		buf.append("dd  d=");	buf.append(sequence[13]);	buf.append("\n");
		buf.append("dd\n");
		buf.append("\n");
		
		buf.append(adjustPin());
		buf.append(adjustPin());
		buf.append("\n");
		buf.append(adjustPin());
		buf.append(adjustPin());
		
		return buf.toString();
	}

}
